// app.js
App({
  onLaunch() {
    // 展示本地存储能力
    wx.cloud.init({
      env: 'cloud1-1g9svrt778565b19',
      traceUser: true,
    })
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
      }
    })
    this.getUserId();
  },
  getUserId: function(){
    if(this.globalData.userInfo){
      return this.globalData.userInfo;
    }
    else{

      var t;
      t = wx.cloud.callFunction({
        name: 'getOpenId',
      }).then(res =>{
        
        var userInfo = res.result.event.userInfo;
        this.globalData.userInfo = userInfo;
      });
      Promise.all([t]).then(res =>{});
      return this.globalData.userInfo;
    }

  },
  getUserId_promise:function(){
    if(this.globalData.userInfo){
      return Promise.resolve(this.globalData.userInfo);
    }
    else{
      let t = wx.cloud.callFunction({
        name: 'getOpenId',
      }).then(res =>{
        let userInfo = res.result.event.userInfo;
        this.globalData.userInfo = userInfo;
        return userInfo;
      });
      return t;
    }
  },
  getWeekTime: function(){
    var week = new Array();
    var today = new Date();
    var length = 7;
    var weekDayToStr = ["周日","周一","周二","周三","周四","周五","周六"];
    for (var i=0;i<length;i++)
  { 
    var t = new Date();
    t.setDate(today.getDate()+i);
    week.push({month: t.getMonth()+1,monthDate: t.getDate(), weekDate: weekDayToStr[t.getDay()] });
  }
    return week;
  },
  globalData: {
    userInfo: null
  }
})
